﻿using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;

namespace DevelopTool.My.Plugins.Common
{
    /// <summary>
    /// 报表加密/解密 帮助类
    /// </summary>
    public class ReportEncryptionHelper
    {
        // Token: 0x04000740 RID: 1856
        private static readonly string IV = "SuFjcEmp/TE=";

        // Token: 0x04000741 RID: 1857
        private static readonly string Key = "KIPSToILGp6fl+3gXJvMsN4IajizYBBT";

        /// <summary>
        /// 报表内容加密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Encrypt(string text)
        {
            TripleDESCryptoServiceProvider cryptoProvider = GetCryptoProvider();
            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] bytes = new global::System.Text.UTF8Encoding().GetBytes(text);
            cryptoStream.Write(bytes, 0, bytes.Length);
            cryptoStream.FlushFinalBlock();
            byte[] inArray = memoryStream.ToArray();
            cryptoStream.Close();
            memoryStream.Close();
            return Convert.ToBase64String(inArray);
        }

        // Token: 0x06000F2A RID: 3882 RVA: 0x0005ABB8 File Offset: 0x00058DB8
        private static TripleDESCryptoServiceProvider GetCryptoProvider()
        {
            return new TripleDESCryptoServiceProvider
            {
                IV = Convert.FromBase64String(IV),
                Key = Convert.FromBase64String(Key)
            };
        }

        /// <summary>
        /// 报表内容解密
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Decrypt(string text)
        {
            using (TripleDESCryptoServiceProvider cryptoProvider = GetCryptoProvider())
            {
                byte[] array = Convert.FromBase64String(text);
                using (MemoryStream memoryStream = new MemoryStream())
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, cryptoProvider.CreateDecryptor(), CryptoStreamMode.Write))
                    {
                        cryptoStream.Write(array, 0, array.Length);
                        cryptoStream.FlushFinalBlock();
                        return new UTF8Encoding().GetString(memoryStream.ToArray());
                    }
                }
            }
        }

    }
}
